/*
 * TreeTransferHandler.java
 */
package com.vs.kindlecoll.UI;

import com.vs.kindlecoll.Beans.Book;
import com.vs.kindlecoll.Beans.BooksCollection;
import com.vs.kindlecoll.IO.KindleInteractor;
import javax.swing.*;
import java.awt.datatransfer.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

public class TreeTransferHandler extends TransferHandler {

    private LinkedList<TreePath> values = null;

    @Override
    public boolean canImport(TransferHandler.TransferSupport info) {
        //TODO: implement for Book array object
        if (!info.isDataFlavorSupported(new Book().getTransferDataFlavors()[0])) {
            return false;
        }
        return true;
    }

    @Override
    protected Transferable createTransferable(JComponent c) {
        //TODO: implement for Book Array object
        return new Book(exportBooks(c)[0]);
    }

    @Override
    public int getSourceActions(JComponent c) {
        return TransferHandler.COPY_OR_MOVE;
    }

    @Override
    public boolean importData(TransferHandler.TransferSupport info) {
        if (!info.isDrop()) {
            return false;
        }

        JTree tree = (JTree) info.getComponent();
        JTree.DropLocation dl = (JTree.DropLocation) info.getDropLocation();
        TreePath path = dl.getPath();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();


        // Get dropped object.
        Transferable t = info.getTransferable();

        Book data = new Book();
        try {
            data = (Book) t.getTransferData(data.getTransferDataFlavors()[0]);
            
            //node.getParent() equal to null means that we are droping between 
            //collections, i.e. to root node. It will be outstanding book.
            if (node.getParent() != null) {
                KindleInteractor.addBookToCollection(
                        (BooksCollection)node.getUserObject(),
                        data);
            UIHelper ui = new UIHelper();
            ui.updateTreeModel(tree);
// TODO: this code is optimal, in future we should return to this instead of updateTreeModel()
//                treeModel.insertNodeInto(new DefaultMutableTreeNode(data), 
//                        node, 1);

            } else {
                //Insert between collections
//                treeModel.insertNodeInto(new DefaultMutableTreeNode(data), 
//                        (DefaultMutableTreeNode)treeModel.getRoot(), 1);
                return false;
                        
            }
        }catch(UnsupportedFlavorException ufe) {  
            System.out.println("UnsupportedFlavor: " + ufe.getMessage());  
        } catch(java.io.IOException ioe) {  
            System.out.println("I/O error: " + ioe.getMessage());  
        }

        return true;
    }

    @Override
    protected void exportDone(JComponent c, Transferable data, int action) {
        
    }

    protected Book[] exportBooks(JComponent c) {
        JTree tree = (JTree) c;
        values = new LinkedList<TreePath>(Arrays.asList(tree.getSelectionPaths()));

        ArrayList<Book> result = new ArrayList<Book>();

        for (TreePath val : values) {
            result.add((Book) ((DefaultMutableTreeNode) val.getLastPathComponent()).getUserObject());
        }

        return (Book[]) result.toArray(new Book[result.size()]);
    }

}
